/**
 * @description:
 * @file: methods.ts
 * @author: Evan
 * @date: 2025.06.24 11:50:02
 * @week: 周二
 * @version: V
 */

import * as THREE from 'three/webgpu'
import { Scene, Utils, Hooks } from 'three-scene'
export { Hooks, Utils, Scene, THREE }

import __CONFIG__ from './config'

import { WaterMesh } from 'three/examples/jsm/objects/WaterMesh.js'

// 创建水面
export const createWater = (model?: InstanceType<typeof THREE.Mesh>) => {
  // 水面
  const size1 = 2574,
    size2 = 685
  const waterGeometry = model ? model.geometry : new THREE.PlaneGeometry(size1, size2)
  const loader = new THREE.TextureLoader().setPath(__CONFIG__.baseUrl)
  // 法线贴图
  const waterNormals = loader.load('/textures/waternormals.jpg')
  // 重复
  waterNormals.wrapS = waterNormals.wrapT = THREE.RepeatWrapping

  const water = new WaterMesh(waterGeometry, {
    // 法线
    waterNormals: waterNormals,
    // 太阳方向
    sunDirection: new THREE.Vector3(),
    // 太阳颜色
    sunColor: 0xffffff,
    // 水面颜色
    waterColor: 0x88b0e5,
    // 扭曲大小
    distortionScale: 3
  })
  return water
}

/**
 * 创建模型动画
 * @param model 模型
 * @param animations 模型动画
 * @param play 播放
 * @param timeScale 加速
 * @returns
 */
export const createModelAnimate = (
  model: InstanceType<typeof THREE.Object3D>,
  animations = [],
  play: boolean = true,
  timeScale: number = 1
) => {
  if (!animations.length) return
  const mixer = new THREE.AnimationMixer(model)
  const obj = animations.reduce((ob, cur: any) => {
    const key = cur.name || ''
    ob[key] = mixer.clipAction(cur)
    if (play) {
      ob[key].play()
    }
    ob[key].timeScale = timeScale
    return ob
  }, {} as AnyObject)

  model.userData.actions = obj
  model.userData.mixer = mixer
}
